<?php
/**
 * Created by PhpStorm.
 * User: SYSTEM
 * Date: 2018-8-16
 * Time: 16:06
 */

/**
 * Class page
 * 基本分页类
 */
class page{
    private $pg_total;      //总记录数
    private $pg_size;       //一页显现出的记录数
    private $pg_page;       //当前页
    private $pg_page_count; //总页数
    private $pg_i;          //起始页数
    private $pg_en;         //结尾页数
    private $pg_url;        //获取当前的url

    /**
     * @var $show_pages
     * 页面显示的格式，显示链接为页数2*$show_pages+1
     * 如$show_pages=2 那么页面上显示就是【首页】【上页】 1 2 3 4 5 【下页】 【尾页】
     */
    private $show_pages;


    public function __construct($pg_total=1, $pg_size=1, $pg_page=1, $pg_url, $show_pages=2){
        $this->pg_total = $this->numeric($pg_total);
        $this->pg_size  = $this->numeric($pg_size);
        $this->pg_page  = $this->numeric($pg_page);
        $this->pg_page_count = ceil($this->pg_total / $this->pg_size);
        $this->pg_url   = $pg_url;

        //如果总记录条数小于0，就置总记录数为0
        if($this->pg_total < 0){
            $this->pg_total = 0;
        }
        //如果当前页数小于1，不存在，则置为第一页
        if($this->pg_page < 1){
            $this->pg_page = 1;
        }
        //如果总页数小于1，那么默认为1页
        if($this->pg_page_count < 1){
            $this->pg_page_count = 1;
        }
        //如果当前页大于总页数，那么当前页就为最后一页
        if($this->pg_page > $this->pg_page_count){
            $this->pg_page = $this->pg_page_count;
        }

        $this->limit = ($this->pg_page - 1) * $this->pg_size;

        //开始页码计算
        $this->pg_i = $this->pg_page - $show_pages;
        //结束页码计算
        $this->pg_en = $this->pg_page + $show_pages;

        //如果初始页小于1，则零初始为1
        if($this->pg_i < 1){
            $this->pg_en = $this->pg_en+(1-$this->pg_i);
            $this->pg_i = 1;
        }

        if($this->pg_en > $this->pg_page_count){
            $this->pg_i = $this->pg_i - ($this->pg_en - $this->pg_page_count);
            $this->pg_en = $this->pg_page_count;
        }

        if($this->pg_i < 1){
            $this->pg_i = 1;
        }

    }

    /**
     * @param $num
     * @return bool|int|string
     * 检测是否为数字
     */
    public function numeric($num){
        if(strlen($num)){
            if(!preg_match("/^[0-9]+$/",$num)){
                $num=1;
            }else{
                $num = substr($num, 0, 11);
            }
        }else{
            $num = 1;
        }

        return $num;
    }

    /**
     * @param $page
     * @return mixed
     * 地址转换
     */
    private function pg_replace($page){
        return str_replace("{page}",$page,$this->pg_url);
    }

    /**
     * @return string
     * 首页
     */
    private function pg_home(){
        if($this->pg_page!=1)
        {
            return '<a href="'.$this->pg_replace(1).'" title="首页">首页</a>';
        }else{
            return '<p>首页</p>';
        }
    }

    /**
     * @return string
     * 上一页
     */
    private function pg_prev(){
        if($this->pg_page!=1){
            return '<a href="'.$this->pg_replace($this->pg_page - 1).'" title="上一页">上一页</a>';
        }else{
            return '<p>上一页</p>';
        }
    }
    /**
     * @return string
     * 下一页
     */
    private function pg_next(){
        if($this->pg_page!=$this->pg_page_count){
            return '<a href="'.$this->pg_replace($this->pg_page + 1).'" title="下一页">下一页</a>';
        }else{
            return '<p>下一页</p>';
        }
    }
    /**
     * @return string
     * 尾页
     */
    private function pg_last(){
        if($this->pg_page!=$this->pg_page_count){
            return '<a href="'.$this->pg_replace($this->pg_page_count).'" title="尾页">尾页</a>';
        }else{
            return '<p>尾页</p>';
        }
    }

    /**
     * @param string $id
     * @return string
     * 打印输出显示
     */
    public function pg_write($id='page'){
        $str = "";
        $str .= '<div id = "'.$id.'">';
        $str .= $this->pg_home();
        $str .= $this->pg_prev();
        if($this->pg_i > 1){
            $str .= '<p class="pageEllipsis">...</p>';
        }
        for($i=$this->pg_i; $i<= $this->pg_en; $i++){
            if($i==$this->pg_page){
                $str.='<a href="'.$this->pg_replace($i).'" title="第'.$i.'页" class="cur">'.$i.'</a>';
            }else{
                $str.='<a href="'.$this->pg_replace($i).'" title="第'.$i.'页">'.$i.'</a>';
            }
        }
        if($this->pg_en < $this->pg_page_count){
            $str .= '<p class="pageEllipsis">...</p>';
        }
        $str .= $this->pg_next();
        $str .= $this->pg_last();
        $str .= '<p class="pageRemark">共<b>'.$this->pg_page_count.'</b>页<b>'.$this->pg_total.'</b>条数据</p>';
        $str.="</div>";
        return $str;
    }

}